import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/Rx';
@Injectable()
export class productService {
    //根据id获取的对象信息  
    product:Product;
    constructor(private http:Http){}

    /**
     * @description 函数用来获取所有的商品数据
     * @return Observable<Product[]>
     */
    getProducts():Observable<Product[]>{
        return this.http.get('/api/products').map(res => res.json());
    }

    /**
     * @description  根据传递的id获取对应id的商品信息
     * @param id  产品id
     * @returns Observable<Product>
     */
    getProduct(id:number):Observable<Product>{
        return this.http.get('/api/product/'+id).map(res => res.json());
    }

    /**
     * @description 根据商品的id获取到对应的商品的评论信息
     * @param id 产品的id
     * @returns Observable<Comment[]>
     */
    getCommentForProduct(id:number):Observable<Comment[]>{
        return this.http.get('/api/product/'+id+'/comments').map(res => res.json());
    }

    /**
     * @description 获取产品的分类
     * @returns string[]
     */
    getCategory(){
        return ['电子产品','软件产品','保健品','食品','书籍','杂志','汽车配件','服饰','男装'];
    }

}

//产品的类
export class Product{
    //构造函数,定义产品类所需要的属性
    constructor(
        public id:number,
        public title:string,
        public price:number,
        public des:string,
        public rating:number,
        public category:Array<string>
    ){}
  
}

//评论的类
export class Comment{
    constructor(
        public id:number,
        public productId:number,
        public timestamp:string,
        public user:string,
        public rating:number,
        public content:string
    ){}
}